package com.android.djlmapp.adapter.home;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.djlmapp.R;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.LinkedList;

public class UploadImageAdapter extends BaseAdapter {


    private LinkedList<String> imagePathList = new LinkedList<>();
    private Context context;
    private boolean isAddData = true;
    /**
     * 控制最多上传的图片数量
     */
    private int imageNumber = 6;

    public UploadImageAdapter(Context context, LinkedList<String> imagePath) {
        this.context = context;
        this.imagePathList = imagePath;
    }

    public void update(LinkedList<String> imagePathList) {
        this.imagePathList = imagePathList;
        //这里控制选择的图片放到前面,默认的图片放到最后面,
        if (isAddData) {
            //集合中的总数量等于上传图片的数量加上默认的图片不能大于imageNumber + 1
            if (imagePathList.size() == imageNumber + 1) {
                //移除默认的图片
                imagePathList.removeLast();
                isAddData = false;
            }
        } else {
            //添加默认的图片
            imagePathList.addLast(null);
            isAddData = true;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (imagePathList != null && imagePathList.size() > 0) {
            return imagePathList.size();
        } else {
            return 0;
        }
    }

    @Override
    public String getItem(int position) {
        if (imagePathList != null && imagePathList.size() > 0) {
            return imagePathList.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(context, R.layout.choose_pic_item, null);

        ImageView iv_image = (ImageView) view.findViewById(R.id.choose_iv_img);

        String str = imagePathList.get(position);
        Log.e("项目异常", "------getView------" + str);
        if (str != null && !TextUtils.isEmpty(str)) {
            Glide.with(context).load(new File(str)).into(iv_image);
        } else {
            Glide.with(context).load(R.drawable.pic_pic).into(iv_image);
        }
        return iv_image;
    }

}
